package com.zhanggm.springcloud.jpa.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;

/**
 * TODO
 * @author Zgm
 * @version 1.0
 * @date 2020/6/27 0027 8:42
 */
@ApiModel(description = "学生的实体类")
@Getter
@Setter
@Entity
@Table(name = "t_student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @ApiModelProperty(value = "学生姓名")
    private String name;
    @Column(name = "classes_id")
    private Integer classesId;
    @Column(name = "provice_id")
    private Integer proviceId;
    private Integer cityId;
    private Integer areaId;

    @Transient
    private String classesName;

    @Transient
    private String hobbyName;

    @Transient
    private Integer hobbyId;

    @Transient
    private List<Integer> ids;

    //关联班级信息
    //级联跟新MERGE、级联保存PERSIST，级联刷新,REFRESH,级联删除REMOVE
    //JoinColumn(name关联字段) referencedColumnName如果是主键，可以省略
    @JsonIgnoreProperties("students")
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.DETACH})
    @JoinColumn(name = "classes_id",referencedColumnName = "id",insertable = false,updatable = false)
    private Classes classes;


    @OneToOne(cascade = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.DETACH})
    @JoinColumn(name = "provice_id",insertable = false,updatable = false)
    private Area provice;


    //关联查询兴趣爱好 有中间表的情况
    // FetchType.EAGER一对多，或多对多，也及时说查询列表数据的时候，需要制定饥饿加载FetchType.EAGER
    @JsonIgnoreProperties("students")
    @ManyToMany(cascade ={CascadeType.MERGE,CascadeType.PERSIST},fetch = FetchType.EAGER)
    @JoinTable(name = "t_sudent_hobby_relation",joinColumns = @JoinColumn(name = "student_id"),inverseJoinColumns = @JoinColumn(name="hobby_id"))
    private List<Hobby> hobbies;


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", classesId=" + classesId +
                ", proviceId=" + proviceId +
                ", cityId=" + cityId +
                ", areaId=" + areaId +
                ", provice=" + provice +
                '}';
    }
}
